package com.example.chapter6;


import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/servletConfigTest")
public class ServletConfigTest extends HttpServlet {
    private ServletConfig config;
    public ServletConfigTest(){
        super();
    }
    @Override
    public void destroy(){
        super.destroy();
    }
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);//获取web.xml配置信息到config
        this.config=config;//将配置信息赋值到给私有属性config
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
            SecurityException, IOException{
        //获取到web.xmlp配置文件中charaset对应的值 uft-8
        String charset= this.config.getInitParameter("charset");
        response.setCharacterEncoding(charset);
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>ServietConfig</title></head>");
        out.println("<body>");
        out.println("获取lnitParamServlet的初始化参数\"encoding\"的字符串值："+charset);
        out.println("</body>");
        out.println("</html>");
        out.flush();
        out.close();
    }

}
